package com.koron.bean.query;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

import java.util.List;

/**
 * @author jzp
 * @since 2021/1/13 10:08
 */
@ToString
@Data
@ApiModel("分页显示对象")
public class Page<T> {
    /**
     * 第几页，默认第一页，offset=0
     */
    @ApiModelProperty(value = "页码，默认第一页")
    protected int page = 1;
    /**
     * 每页条数
     */
    @ApiModelProperty(value = "每页数据条数，默认15")
    protected int pageCount = 15;

    @ApiModelProperty(hidden = true)
    private int offset = 0;
    @ApiModelProperty(hidden = true)
    private int limit = 15;

    /**
     * 数据总条数
     */
    @ApiModelProperty(value = "查询的数据总条数")
    protected int total;

    /**
     * 数据总页数
     */
    @ApiModelProperty(value = "查询的数据总页数")
    private int totalPage;

    /**
     * 排序参数
     */
    @ApiModelProperty(value = "待排序字段名")
    private String orderColumn;

    /**
     * 是否降序排列，默认 false
     */
    @ApiModelProperty(value = "是否降序排列，默认 false")
    private boolean desc = false;

    /**
     * 分页数据
     */
    @ApiModelProperty(value = "查询的结果集", hidden = false)
    private List<T> records;

    public Page() {
    }

    public Page(int page, int pageCount) {
        setPage(page);
        setPageCount(pageCount);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
        setOffset((page - 1) * pageCount);
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
        setOffset((page - 1) * pageCount);
        setLimit(pageCount);
    }

    public void setTotal(int total) {
        this.total = total;
        if (total % pageCount == 0) {
            setTotalPage(total / pageCount);
        } else {
            setTotalPage(total / pageCount + 1);
        }
    }

    public void setOrderColumn(String orderColumn) {
        // 驼峰命名转下划线命名
        this.orderColumn = StrUtil.toUnderlineCase(orderColumn);
    }


}
